package formal.hash;

import java.util.HashSet;

/**
 * @author DengYuan2
 * @create 2021-01-30 21:06
 */
public class E_217 {
    public static void main(String[] args) {
        int[] a ={1,2,3,1};
        boolean res = containsDuplicate(a);
        System.out.println(res);
    }

    /**
     * 我的写法
     * @param nums
     * @return
     */
    public static boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])){
                return true;
            }
            set.add(nums[i]);
        }
        return false;
    }

    /**
     * 大神的写法-感觉还没我的快
     * @param nums
     * @return
     */
    public static boolean containsDuplicate2(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num:nums){
            set.add(num);
        }
        return set.size()<nums.length;
    }
}
